package session.stateless;

import entity.Role;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class RoleSessionBean implements RoleSessionBeanLocal {
    @PersistenceContext 
    private EntityManager entityManager; 

    @Override
    public String getRoleDescription(Long roleId){
        Query query = entityManager.createQuery("SELECT r.description FROM Role r WHERE r.id = :inRoleId"); 
        query.setParameter("inRoleId", roleId);
        Role role = null; 
         
        try 
        { 
            role = (Role)query.getSingleResult(); 
        } 
        catch(NoResultException ex) 
        { 
            ex.printStackTrace(); 
        } 
        return role.getDescription();
    } 
    
    @Override
    public List<Role> getRoles(){
        Query query = entityManager.createQuery("SELECT r FROM Role r"); 
        return query.getResultList();
    } 
}
